<?php
class share {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function share() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayListForm($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		
		if(isset($formvars['page']) && is_numeric($formvars['page'])){
			$page = $formvars['page'];
		}else{
			$page = 1;
		}
		
		$limit = "limit ".($page-1)*PAGE_SIZE.",".PAGE_SIZE;
		
		
		/**
		 * 総件数
		 */
	
		$sql = "SELECT COUNT(site_id) FROM site_mst";
		$stmt = $this->db->prepare($sql);	
		$stmt->execute();
		$count =$stmt->fetchColumn();
		$this->smarty->assign('count', $count);
		$stmt = null;
		
		/**
		 * 一覧
		 */
		
		$sql = "SELECT * FROM site_mst ORDER BY site_id  $limit";
		$stmt = $this->db->prepare($sql);
		$stmt->execute();

		$sharelist = $stmt->fetchAll();
		
		$stmt = null;
		
		$this->smarty->assign('sharelist', $sharelist);
		
		$this->smarty->assign('paging', $this->smarty->getNavigation("share_index.php",$page,$count,''));
		$this->smarty->assign('msg', $this->msg);
		$this->initDropDown();

		$this->smarty->display( 'share_index.tpl' );
	}
	
	function deletSite($formvars = array())
	{
		$site_id=$formvars['site_id'];
		$count =$this->db->exec("DELETE FROM site_mst WHERE site_id = '$site_id'");
		if($count>0)
		{
			$this->msg = "共有サイト[$site_id]の削除に成功しました";
		}
		else
		{
			$this->msg = "共有サイト[$site_id]を削除できません";
		}
		
	}
	
	function displayEditForm($formvars = array())
	{
		$site_id = $formvars['site_id'];
		$sql = "SELECT * FROM site_mst WHERE site_id = :site_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':site_id', $site_id);
		$stmt->execute();
		$result = $stmt->fetch();
		if($result != null)
		{
			$this->smarty->assign('rslt', $result);
		}
		else
		{
			$this->smarty->assign('fatal', true);
			$this->msg="共有サイト[$site_id]は探せません";
		}
		$stmt = null;
		$this->smarty->assign('formvars',$formvars);
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'share_edit.tpl' );
	}
	
	function doUpdate($formvars = array())
	{
		$site_id = $formvars['site_id'];
		$sql = "
		Update site_mst set 
		site_name=:site_name,site_url=:site_url,open_flag=:open_flag,
		reg_url=:reg_url,reg_flashobj=:reg_flashobj,v_type=:v_type 
		where site_id =:site_id";
		//echo $sql;
		
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':site_name', $formvars['site_name']);
		$stmt->bindParam(':site_url', $formvars['site_url']);
		$stmt->bindParam(':open_flag', $formvars['open_flag']);
		$stmt->bindParam(':reg_url', $formvars['reg_url']);
		$stmt->bindParam(':reg_flashobj', $formvars['reg_flashobj']);
		$stmt->bindParam(':v_type', $formvars['v_type']);
		$stmt->bindParam(':site_id', $site_id);
		$flag = $stmt->execute();

		if($flag )
		{
			$this->msg = "共有サイト[$site_id]の更新に成功しました";
		}
		else
		{
			$this->msg = "共有サイト[$site_id]の更新に失敗しました";
		}
		
	}
	
	function doInsert($formvars = array())
	{
		$sql = "Insert into
		        site_mst(site_name,site_url,open_flag,reg_url,reg_flashobj,v_type)
		        values(:site_name,:site_url,:open_flag,:reg_url,:reg_flashobj,:v_type)";
				
	  //  echo $sql;
		 
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':site_name', $formvars['site_name']);
		$stmt->bindParam(':site_url', $formvars['site_url']);
		$stmt->bindParam(':open_flag', $formvars['open_flag']);
		$stmt->bindParam(':reg_url', $formvars['reg_url']);
		$stmt->bindParam(':reg_flashobj', $formvars['reg_flashobj']);
		$stmt->bindParam(':v_type', $formvars['v_type']);
		$flag = $stmt->execute();
		if($flag)
		{
			$this->msg = "共有サイトの登録に成功しました";
		}
		else
		{
			$this->msg = "共有サイトの登録ができません";
		}
		
	}
	
	function initDropDown()
	{		
		$this->smarty->assign('openFlagList', array(
                                '1' => '公開',
                                '0' => '非公開'));
								
	    $this->smarty->assign('vTypeList', array(
                                '1' => 'flv',
                                '2' => 'wmv',
								'3' => 'mpeg',
								'4' => 'avi',
								'0' => 'others'));
	}
	
}
?>